# coding=utf-8

import os
import shutil
import re

class CopyDatabasic():
    __src_deep = 0
    __dst_deep = 0
    __src_path = None
    __dst_path = None
    __destination = None
    __source = None
    __file_list = []
    __folder_list = []
    __specify_file_list = []
    __specify_folder_list = []
    
    def __init__(self,src,dst):
        self.__src_path = src
        self.__dst_path = dst
        self.__src_deep = len(src.split('\\'))
        self.__dst_deep = len(dst.split('\\'))

    def __build_dir(self):
        src_list = self.__source.split('\\')
        cur_src_deep = len(src_list)
        cur_dst_deep = len(self.__destination.split('\\'))
        for i in range(cur_src_deep-self.__src_deep):
            if (cur_dst_deep-self.__dst_deep)==(cur_src_deep-self.__src_deep):
                break
            folder = src_list[self.__src_deep+i]
            self.__destination = self.__destination + '\\' + folder
            if not os.path.isdir(self.__destination):
                os.mkdir(self.__destination) 
    
    def regex_check(self,pattern,string):
        reobj = re.compile(pattern)
        search_obj = reobj.search(string)
        if search_obj:
            print  search_obj.group()
            return search_obj.group()
        return None

    def copy_data(self):
        for info in os.walk(self.__src_path):
            all_path,folder_names,file_names = info
            self.__destination = self.__dst_path
            self.__source = all_path
            for folder in folder_names:
                if not folder in self.__folder_list:
                    continue
                src_path = os.path.join(all_path,folder)
                for specify_path in self.__specify_folder_list:
                    if self.regex_check(specify_path,src_path):
                        self.__build_dir()
                        dst_path = os.path.join(self.__destination,folder)
                        self.copy_tree(src_path,dst_path) 
            for file in file_names:
                if not file in self.__file_list:
                    continue
                src_path = os.path.join(all_path,file)
                for specify_path in self.__specify_file_list:
                    if self.regex_check(specify_path,src_path):
                        self.__build_dir()
                        dst_path = os.path.join(self.__destination,file)
                        self.copy_file(src_path,dst_path)

    # 文件名称List
    def set_file(self,flist):
        self.__specify_file_list = flist
        for f in flist:
            name = os.path.split(f)[-1]
            self.__file_list.append(name)

    # 文件夹名称List
    def set_folder(self,flist):
        self.__specify_folder_list = flist
        for f in flist:
            name = os.path.split(f)[-1]
            self.__folder_list.append(name)
            
    # 拷贝的文件夹
    def copy_tree(self,src,dst):
        # cmd='xcopy %s %s /Y /E /T'%(src,dst)
        # os.system(cmd)
        shutil.copytree(src,dst)

    # 拷贝的文件  
    def copy_file(self,src,dst):
        cmd='copy %s %s /Y'%(src,dst)
        os.system(cmd)


def TransDir(path,path2,callbackfun):
    sdeep = len(path.split('\\'))
    ddeep = len(path2.split('\\'))
    for info in os.walk(path):
        dirpath,filepath,filenames = info
        newpath = path2
        for filename in filenames:
            # 构建目录
            cplist = dirpath.split('\\')
            csdeep = len(cplist)
            cddeep = len(newpath.split('\\'))
            for i in range(csdeep-sdeep):
                if (cddeep-ddeep)==(csdeep-sdeep):
                    break
                folder = cplist[sdeep+i]
                newpath = newpath + '\\' + folder
                if not os.path.isdir(newpath):
                    os.mkdir(newpath)
            # end
            src = os.path.join(dirpath,filename)
            dst = os.path.join(newpath,filename.split('.')[0])
            callbackfun(src,dst)

# 递归遍历目录
def GetAllDir(path,filefunc=None,dirfunc=None):
    filelist = os.listdir(path)
    for filename in filelist:
        fileabspath = os.path.join(path, filename)
        if os.path.isdir(fileabspath):
            if dirfunc != None:
                if dirfunc(path,filename) == False:
                    continue
            GetAllDir(fileabspath,filefunc,dirfunc)
        else:
            if filefunc != None:
                filefunc(path,filename)

def RenameFile(path, filename):
    if not filename in ["Test.txt"]:
        return True
    src = os.path.join(path, filename)
    dst = os.path.join(path, filename.split('.')[0]+'.idx')
    if os.path.isfile(dst):
        os.remove(dst)
    os.rename(src,dst)
    return True
    

if __name__ == '__main__':
    inpath = r'G:\newFolder'.decode('utf-8').encode('gbk')
    output = r'G:\Audio\newFolder'.decode('utf-8').encode('gbk')
    if not os.path.isdir(output):
        os.mkdir(output)
    copy = CopyDatabasic(inpath,output)
    copy.set_file([r'seg1.ts']) # 指定拷贝的文件
    copy.set_folder([r'mp4\\(\d+)\\N8']) # 指定拷贝的目录
    copy.copy_data()

    # GetAllDir(inpath,filefunc=RenameFile) # 批量重命名文件